package servlet;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DoLogin extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DoLogin() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	static class MyAuthenricator extends Authenticator{  
        String user=null;  
        String pass="";  
        public MyAuthenricator(String user,String pass){  
            this.user=user;  
            this.pass=pass;  
        }  
        @Override  
        protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication(user,pass);  
        }  
          
    }  
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		String result;
		String username = request.getParameter("username");
		String password = request.getParameter("password");
        Properties props = new Properties();      
        props.put("mail.smtp.host", "dinosaur.com"); // smtp服务器      
        props.put("mail.smtp.auth", "true"); // 是否smtp认证      
        props.put("mail.smtp.port", "25"); // 设置smtp端口      
        props.put("mail.transport.protocol", "smtp"); // 发邮件协议      
        props.put("mail.store.protocol", "pop3"); // 收邮件协议      
        // 获取会话      
        Session mailsession = Session.getInstance (props, null);      
        // 获取Store对象，使用POP3协议，也可能使用IMAP协议      
        HttpSession session = request.getSession();    
        //拿session的时候传入Authenticator子类进行验证  
        try{
            
        	System.out.println("before");
//        	Session mailsession=Session.getDefaultInstance(mailProps,new MyAuthenricator(username,password));
        	Store store = mailsession.getStore("pop3");
        	   // 使用split() 方法把 @ 号左右部分分开并保存到数组中
            String[] tmp = username.split("@");
         // 数组第一个元素就是邮箱用户名
            username = tmp[0];
         // 数组第二个元素就是邮箱域名
            String host= tmp[1];
            store.connect(host, username, password);
        		
        	request.setAttribute("username", username);
        	request.setAttribute("password", password);
        	request.setAttribute("host", host);
        	System.out.println("host:"+host);
        	System.out.println("username:"+username);
        	request.getRequestDispatcher("mailbox.jsp").forward(request, response);
        	System.out.println("after");

        }catch(Exception e){
        	   result ="登陆失败，请重新登陆";
    			request.setAttribute("result", result);
    			request.getRequestDispatcher("index.jsp").forward(request, response);
        }
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.sendRedirect("/Mail/index.jsp");
        
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}


